package org.sky.action;

import com.opensymphony.xwork2.ModelDriven;
import org.sky.model.Evaluate;
import org.sky.model.FileUtil;
import org.sky.service.EvaluateService;
import org.sky.service.FileUtilService;
import org.sky.vo.EvaluateVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-11-7
 * Time: 下午6:00
 * To change this template use FileUtil | Settings | FileUtil Templates.
 */
@Component("evaluate")
@Scope("prototype")
public class EvaluateAction extends MyActionSupport implements ModelDriven<EvaluateVO> {

    private EvaluateVO evaluateVO = new EvaluateVO();
    private FileUtilService fileUtilService;
    private EvaluateService evaluateService;
    private FileUtil fileUtil = new FileUtil();


    @Override
    public String execute() {
        List<FileUtil> fileUtils = fileUtilService.getScrollData(FileUtil.class).getResultList();
        if (fileUtils.size() == 0) {
            evaluateVO.setResultMessage("<script>alert('暂时还没有音乐！');location.href='/page/file/uploadFile.jsp';</script>");
            return ERROR;
        }

        //跳转到某首歌曲
        if(evaluateVO.getFileId() != 0){
            fileUtil = fileUtilService.find(FileUtil.class,evaluateVO.getFileId());
        }else {
            fileUtil = fileUtils.get(0);
        }

        //取得评论
        List<Evaluate> evaluates = evaluateService.findEvaluates(fileUtil.getId());
        evaluateVO.setEvaluates(evaluates);
        evaluateVO.setMusicFile(fileUtil);
        evaluateVO.setFileUtils(fileUtils);
        view = "/page/evaluate/evaluate.jsp";
        return VIEW;
    }


    @Override
    public EvaluateVO getModel() {
        return evaluateVO;
    }

    @Resource(name = "fileUtilServiceImpl")
    public void setFileUtilService(FileUtilService fileUtilService) {
        this.fileUtilService = fileUtilService;
    }

    @Resource(name = "evaluateServiceImpl")
    public void setEvaluateService(EvaluateService evaluateService) {
        this.evaluateService = evaluateService;
    }

}
